<template>
  <div class="login">
    <div class="login-box">
      <div class="login-box-left">
      </div>
      <div class="login-box-right">
        <div class="right">
          <div class="title">
            凯诘绩效管理系统
          </div>
          <div class="input">
            <Form
              ref="loginForm"
              :model="loginForm"
              :rules="ruleValidate"
            >
              <FormItem prop="userId">
                <Input
                  class="login-input"
                  v-model="loginForm.userId"
                  placeholder="请输入工号"
                  @keyup.enter.native="handleLoginSubmit('loginForm')"
                ></Input>
              </FormItem>
              <FormItem prop="passWord">
                <Input
                  class="login-input"
                  type="password"
                  v-model="loginForm.passWord"
                  placeholder="请输入密码"
                  :passWord="true"
                  @keyup.enter.native="handleLoginSubmit('loginForm')"
                ></Input>
              </FormItem>
              <FormItem>
                <Button
                  style="width:100%;"
                  type="primary"
                  @click.native.prevent="handleLoginSubmit('loginForm')"
                  :loading="loading"
                >登录</Button>
              </FormItem>
            </Form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      name: "Login",
      loginForm: {
        userId: "",
        passWord: ""
      },
      loading: false,
      redirect: undefined,
      ruleValidate: {
        userId: [
          {
            required: true,
            message: "请输入员工编号",
            trigger: "blur"
          }
        ],
        passWord: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    async handleLoginSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          this.loading = true;
          let formData = {};
          formData.passWord = this.loginForm.passWord;
          formData.userId = this.loginForm.userId;
          if (this.loading) {
            this.$store
              .dispatch("Login", formData)
              .then(res => {
                this.loading = false;
                this.$router.push({ path: this.redirect || "/" });
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      });
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  }
};
</script>
<style lang="less" scoped>
.login {
  position: relative;
  &-box {
    position: absolute;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    display: flex;
  }
  .login-box-left {
    width: 50%;
    min-height: calc(100vh);
    background: url("~@/assets/login-bg.png") no-repeat;
    background-size: 100% 100%;
  }
  .login-box-right {
    width: 50%;
    min-height: calc(100vh);
    overflow: hidden;
    padding: 207px 0 0 130px;
    .title {
      font-weight: 500;
      font-size: 24px;
      color: #cb2d00;
      padding-bottom: 16px;
      border-bottom: 3px solid #cb2d00;
    }
    .input {
      margin-top: 60px;
      width: 300px;
    }
  }
  .login-btn {
    width: 100%;
    font-size: 14px;
  }
}
</style>
<style>
.login .ivu-input {
  height: 36px;
}
</style>